<?php

namespace app\modules\work\controllers;

use yii;
use yii\helpers\Json;
use app\common\Controller;
use app\common\SystemEvent;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii2mod\editable\EditableAction;
use yii2mod\rbac\filters\AccessControl;
use app\models\SemLog;
use app\models\SemLogSearch;
use app\models\CustomTableViewSearch;
use app\models\CustomTableView;

use yii\data\Pagination;
use yii\data\ActiveDataProvider;
use app\common\Functions;



class SemController extends Controller
{
    
	public $module;
	public $protected_uids = [];    
	public $superadmin_uid = 0;
	public $loginRedirect = '/admin/dashboard';
	public $defaultAction = 'index';
	public function init()
	{
		parent::init();
	}


    /*
	 * 更新内容
	 *
	 * */
    public function actionUpdate($id=null,$last_url=null,$wx = null){


        // 初始化最重要的产品实例字段
        $postModel   = new SemLog();
        $model =  SemLog::find()->where(['id'=>$id])->one();




        if ($postModel->load(Yii::$app->request->post())
            && $postModel->validate() )
        {

//            $model->modify_date        = date("Y-m-d H:i:s",time());;
//            $model->modify_staff_id   = Yii::$app->user->id;




            Yii::$app->session->setFlash('success', Yii::t('region', 'Been saved.'));


            $this->insertOrUpdateToTable($postModel, $wx, $model);


                return $this->redirect($last_url);




        }




        return $this->renderAjax('create',
            [
                'update'  => 1,
                'wx'  => $wx,
                'model' => $model
            ]);




    }




    /*
	 * 删除本条记录
	 *
	 * */
    public function actionDelete($id=null, $last_url=null){

        try{
            $this->delete($id);

            Yii::$app->session->setFlash('success', Yii::t('region', 'Been saved.'));
        }catch (IntegrityException $e){
            Yii::$app->session->setFlash('success', Yii::t('region', '删除失败.'));
            //  return $this->redirect(Yii::$app->request->getReferrer());
        }

        if ($last_url)
        {
            return $this->redirect($last_url);
        }

        return $this->redirect(Yii::$app->request->referrer);
    }


     public function actionIndex()
    {
        // 获取当前的url $last_url
        $now_url = Yii::$app->request->getUrl();

        $searchModel = new SemLogSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

//        // 拥有的全部特性
//        $attrSpecs         = Functions::getAttrSpecRelas($spec_id);
//        $attrSpecsDownList = Functions::getAttrSpecDownList($attrSpecs);

         return $this->render('index',
             [
                 'now_url'  => $now_url,
                 'dataProvider' => $dataProvider,
                 'searchModel' =>  $searchModel]);
    }


    public function actionCreate($last_url=null, $wx=null)
    {


        // 初始化类型
        $model = new SemLog();


        if ($model->load(Yii::$app->request->post())
            && $model->validate() )
        {

            $model->create_date        = date("Y-m-d H:i:s",time());;
            $model->create_staff_id   = Yii::$app->user->id;
            $model->modify_date        = date("Y-m-d H:i:s",time());;
            $model->modify_staff_id   = Yii::$app->user->id;


            Yii::$app->session->setFlash('success', Yii::t('region', 'Been saved.'));

            //创建Region
            $this->insertOrUpdateToTable($model,$wx);

            return $this->redirect($last_url);

        }


        return $this->renderAjax('create',
            [

            'wx'  => $wx,
                'last_url'  => $last_url,
                'model' => $model
           ]);
    }

    public function insertOrUpdateToTable( $newModel, $wx=null, $oldModel=null)
    {
        if($oldModel)
        {
          //  $model = SemLog::find()->where(['id'=>$newModel->id])->one();
            if ($oldModel)
            {
                if (!$wx)
                {
                    $oldModel->yq_day_amount = $newModel->yq_day_amount;
                    $oldModel->yq_day_fee = $newModel->yq_day_fee;
                    $oldModel->tb_day_amount = $newModel->tb_day_amount;
                    $oldModel->tb_day_fee = $newModel->tb_day_fee;
                    $oldModel->sq_day_amount = $newModel->sq_day_amount;
                    $oldModel->sq_day_fee = $newModel->sq_day_fee;
                    $oldModel->yx_day_amount = $newModel->yx_day_amount;
                    $oldModel->yx_day_fee = $newModel->yx_day_fee;

                    $oldModel->tbsq_day_amount   = $newModel->sq_day_amount;
                    $oldModel->tbsq_day_fee      = $newModel->sq_day_fee;
                    $oldModel->tbyx_day_amount  = $newModel->yx_day_amount;
                    $oldModel->tbyx_day_fee      = $newModel->yx_day_fee;


                    $oldModel->url = $newModel->url;
                    $oldModel->wt_day_amount = $newModel->wt_day_amount;
                    $oldModel->qd_day_amount = $newModel->qd_day_amount;
                    $oldModel->day_income = $newModel->day_income;
                    $oldModel->day_area = $newModel->day_area;
                    $oldModel->data_date = $newModel->data_date;
                    $oldModel->modify_date = date("Y-m-d H:i:s", time());
                    $oldModel->modify_staff_id = Yii::$app->user->id;
                }
                else
                {
                    $oldModel->fans_amount = $newModel->fans_amount;
                    $oldModel->article_amount = $newModel->article_amount;
                    $oldModel->publish_amount = $newModel->publish_amount;
                    $oldModel->readed_fee = $newModel->readed_fee;
                    $oldModel->repeat_amount = $newModel->repeat_amount;
                    $oldModel->channel_amount = $newModel->channel_amount;
                    $oldModel->advice_amount = $newModel->advice_amount;
                    $oldModel->modify_date = date("Y-m-d H:i:s", time());
                    $oldModel->modify_staff_id = Yii::$app->user->id;

                }
//                Yii::trace("==========wx========");
//                Yii::trace($wx);
//                Yii::trace($oldModel->advice_amount);

                $oldModel->save();
            }

//            if ($model->custom_type!=1)
//            {
//                $model->sort                   = $newModel->sort;
//                $model-> attr_spec_width     = $newModel->attr_spec_width;
//                $model-> attr_spec_id         = $newModel->attr_spec_id;
//                $model-> show_name            = $newModel->show_name ;
//                $model->custom_type = 0;
//            }
//            else
//            {
//                $model->sort                   = $newModel->sort;
//                $model-> attr_spec_width     = $newModel->attr_spec_width;
//                $model-> attr_spec_id         = 0;
//                $model-> show_name            = $newModel->show_name ;
//                $model->custom_type = 1;
//            }

        }
        else
        {
            // 检查是否已经更新过
            $model = SemLog::find()->where(['data_date'=>$newModel->data_date])->one();

            //如果已经更新过则进行更新处理
            if ($model)
            {
                //
                if ($wx)
                {
                    $model->fans_amount = $newModel->fans_amount;
                    $model->article_amount = $newModel->article_amount;
                    $model->publish_amount = $newModel->publish_amount;
                    $model->readed_fee = $newModel->readed_fee;
                    $model->repeat_amount = $newModel->repeat_amount;
                    $model->channel_amount = $newModel->channel_amount;
                    $model->advice_amount = $newModel->advice_amount;
                    $model->modify_date    = date("Y-m-d H:i:s",time());
                    $model->modify_staff_id   = Yii::$app->user->id;

                }
                else
                {
                    $model->yq_day_amount  = $newModel->yq_day_amount;
                    $model->yq_day_fee      = $newModel->yq_day_fee;
                    $model->sq_day_amount   = $newModel->sq_day_amount;
                    $model->sq_day_fee      = $newModel->sq_day_fee;
                    $model->yx_day_amount  = $newModel->yx_day_amount;
                    $model->yx_day_fee      = $newModel->yx_day_fee;

                    $model->tb_day_amount  = $newModel->tb_day_amount;
                    $model->tb_day_fee      = $newModel->tb_day_fee;
                    $model->tbsq_day_amount   = $newModel->sq_day_amount;
                    $model->tbsq_day_fee      = $newModel->sq_day_fee;
                    $model->tbyx_day_amount  = $newModel->yx_day_amount;
                    $model->tbyx_day_fee      = $newModel->yx_day_fee;
                    $model->wt_day_amount  = $newModel->wt_day_amount;
                    $model->qd_day_amount = $newModel->qd_day_amount;
                    $model->day_income     = $newModel->day_income;
                    $model->day_area       = $newModel->day_area;
                    $model->data_date      = $newModel->data_date;
                    $model->modify_date    = date("Y-m-d H:i:s",time());
                    $model->modify_staff_id   = Yii::$app->user->id;
                }

                $model->save();
            }
            else
            {
                $newModel->save();
            }

        }
    }

    public function delete($id)
    {
        $semLog = SemLog::find()->where(['id'=>$id])->one();
        if($semLog)
        {
            $semLog->delete();
        }
    }

    public function actionUrl($id=null)
    {

        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        // 初始化最重要的产品实例字段
        $postModel = new SemLog();
        $model = SemLog::find()->where(['id' => $id])->one();

        return $model;
    }


}
